package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;

   @Operation(summary = "微信小程序登录")
    @GetMapping("/wxLogin/{code}")
    public Result wxLogin(@PathVariable String code) {
        log.info("微信小程序登录，code：{}", code);
       Map<String, Object> map = userInfoService.wxLogin(code);
       return Result.ok(map);
    }
    @TsLogin
    @Operation(summary = "获取用户信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        Long userId = AuthContextHolder.getUserId() ;
        UserInfoVo userInfoVo=userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }
    @TsLogin
    @Operation(summary = "修改个人信息")
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfoVo userInfoVo) {
        Long userId = AuthContextHolder.getUserId();

        userInfoService.updateUserInfo(userInfoVo,userId);
        return Result.ok();
    }
}
